import { getJSON } from '@quickearth/core'
import dayjs from 'dayjs'
// import apiList from '../api/api'
import apiList from '@/pages/login/api/api'
const defaultData = () => ({
  permissions: [],
  // 地图加载
  mapLoading: false,
  moduleVisible: {
    //站点
    zdModule: true,
    //网格
    wgModule: false
  },
  componentsVisible: {
    // 国家站
    gjz: false,
    // 区域站
    qyz: false,
    // 站点名称
    zdmc: false,
    // 站号
    zh: false,
    // 县名
    xm: false,
    labels: '全部',
    //站点
    zd: false,
    //网格
    wg: false,
    fsgdz: false,
    //色斑图
    sbt: false
  },
  bottomBarList: undefined,
  stationInfo: [],
  zdskys: '降水',
  zdskjcCurrentSelect: '',
  zdskjcCurrentTime: '',
  // 站点实况监测 当前站点信息
  // 分钟数据
  zdskjcDataList: [],
  // 小时数据
  zdskjcHourDataList: {
    rain: [],
    tem: []
  },
  sbGdCurrentSelect: '站点',
  regional: '',
  gdskjcOptions: [
    {
      name: '气温',
      value: 'TEM',
      legendStyle: 'temp',
      legendStops: [],
      unit: '℃'
    },
    {
      name: '降水',
      value: 'RAIN',
      legendStyle: 'precp',
      legendStops: [],
      unit: 'mm'
    },
    {
      name: '相对湿度',
      value: 'RH',
      legendStyle: 'rh',
      legendStops: []
    },
    {
      name: '气压',
      legendStyle: 'pslp-low',
      legendStops: [],
      value: 'PRs'
    },
    {
      name: '风',
      value: 'WIND',
      legendStyle: 'wind',
      legendStops: [],
      unit: 'm/s'
    },
    {
      name: '能见度',
      value: 'VIS',
      legendStyle: 'vis',
      legendStops: [],
      unit: 'm'
    }
  ],
  gdskjcCurrentSelect: 'none',
  NavActive: '',
  // 所有数据
  gdskjcAllDataList: [],
  gdskjcDataList: {},
  gdskjcCurrentTime: '',
  // 地图加载
  ntxqhAllData: [],
  ntxqhCurrentSelect: '',
  nyztGdskCurrSelect: 'none'
})

export const useMonitoringStore = defineStore({
  id: 'monitoring',
  state: () => {
    return defaultData()
  },
  getters: {
    // 站点实况监测 当前选中单位
    zdskjcCurrentSelectUnit: state =>
      state.gdskjcOptions?.find(i => i.value === state.zdskjcCurrentSelect)
        ?.unit || '',
    zdskjcCurrentSelectType: state =>
      state.gdskjcOptions?.find(i => i.value === state.zdskjcCurrentSelect)
        ?.type || ''
  },
  actions: {
    async handleGetStationInfo() {
      this.mapLoading = true
      this.mapLoading = false
      this.componentsVisible.gjz = true
    },
    async permissionsInfo() {
      apiList.getInfo().then(res => {
        console.log(res)
        this.permissions = res.permissions
      })
    },
    async handleChoroplethicMapPath() {},
    handleGetGridPointMonitoringLegendData() {
      this.gdskjcOptions.forEach(async item => {
        if (item.legendStyle) {
          let legendStyle = item.legendStyle
          if (legendStyle.indexOf('-'))
            legendStyle = legendStyle.replace('-', '.')
          if (legendStyle == 'wind') {
            item.legendStops = await getJSON(
              `/public/demos/styles/${legendStyle}.color.rules_new.json`
            )
          } else {
            item.legendStops = await getJSON(
              `/public/demos/styles/${legendStyle}.color.rules.json`
            )
          }
        }
      })
    },
    // 销毁 并初始化数据
    handleUnmountedInitData() {
      const defaultDataObj = defaultData()
      Object.keys(defaultDataObj).forEach(item => {
        this[item] = defaultDataObj[item]
      })
    }
  }
})
